include "./global.f95"
module routine
    use variables
    interface output
        module procedure output_int
        module procedure output_real
        module procedure output_real_array
    end interface
contains
    subroutine init
        integer(kind=4) :: i
        t=0
        forall(i=1:NX)
            x(i)=i*Dx+x_range(1)
        end forall
        !u=0.0
        !where(x<0) u=1.0
        u1=5*exp(-(x/2.0)**2)
        u2=u1
        call getB
        reccount=1
    end subroutine
    
    subroutine getB
        implicit none
        b = 0.5
    end subroutine
    
    subroutine DF
        real(kind=4) :: ut(1:NX)
        ut=u1
        u1=u2
        dt=CFL*Dx**2/2.0/maxval(abs(b))
        u2(2:NX-1)=u1(2:NX-1)+b(2:NX-1)*dt/Dx**2*(u1(3:NX)-2*u1(2:NX-1)+u1(1:NX-2))
        u2(1)=u2(2)
        u2(NX-1)=u2(NX)
!~         u2(2:NX-1)=ut(2:NX-1)+2.0*b(2:NX-1)*dt/Dx**2*(u1(3:NX)-u2(2:NX-1)-ut(2:NX-1)+u1(1:NX-2))
!~         u2(1)=u2(2)
!~         u2(NX-1)=u2(NX)
        t=t+dt
    end subroutine

    subroutine output_int(u,x)
        implicit none
        integer :: u
        integer(kind=4) :: x
        write(unit=u,rec=reccount)x
        reccount=reccount+1
    end subroutine

    subroutine output_real(u,x)
        implicit none
        integer :: u
        real(kind=4) :: x
        write(unit=u,rec=reccount)x
        reccount=reccount+1
    end subroutine

    subroutine output_real_array(u,x)
        implicit none
        integer :: u, i
        real(kind=4) :: x(NX)
        do i=1,NX
            write(unit=u,rec=reccount)x(i)
            reccount=reccount+1
        end do
    end subroutine
end module
